<?php  if( !defined("IN_IA") ) 
{
	exit( "Access Denied" );
}
class Slwxkf_EweiShopV2Model
{
    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function __construct
     * @return null
     */
    function __construct()
    {
        $this->query = load()->object('query');
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 更新数据
     * @table string 表名
     * @update array data
     * @where array 条件
     * @return result(1 \\ false)
     */
    public function update($table = '', $update = '', $where = '')
    {
        $result = pdo_update('ewei_shop_'.$table, $update, $where);
        return $result;
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 添加数据
     * @table string 表名
     * @insert array data
     * @return result(1 \\ false)
     */
    public function insert($table = '', $insert = '')
    {
        $result = pdo_insert('ewei_shop_'.$table, $insert);
        if (!empty($result)) {
            return pdo_insertid();
        }
        return $result;
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 删除数据
     * @table string 表名
     * @where array 条件
     * @return result(int \\ false)
     */
    public function delete($table = '', $where = '')
    {
        $result = pdo_delete('ewei_shop_'.$table, $where);
        return $result;
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据条件查找一条数据
     * @table string 表名
     * @where array 条件
     * @field array 查找字段
     * @return result(array,一维)
     */
    public function getInfo($table = '', $where = '', $field = '*')
    {
        if (empty($where)){
            $data = $this->query->from('ewei_shop_'.$table)->select($field)->get();
        }else{
            $data = $this->query->from('ewei_shop_'.$table)->where($where)->select($field)->get();
        }
        return $data;
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据条件查找所有数据
     * @table string 表名
     * @where array 条件
     * @field array 查找字段
     * @return result(array,多维)
     */
    public function getInfoAll($table = '', $where = '', $field = '*')
    {
        if (empty($where)){
            $data = $this->query->from('ewei_shop_'.$table)->select($field)->getall();
        }else{
            $data = $this->query->from('ewei_shop_'.$table)->where($where)->select($field)->getall();
        }
        return $data;
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据会员openid查找每个季度的订单
     * @openid string 会员openid
     * @uniacid string 公众号ID
     * @return result(string,订单总额)
     */
    public function secondaryDistribution($goods)
    {
        global $_W;
        //本会员
        $member = $this->getInfo('member',array('uniacid' => $_W['uniacid'],'id' => $_W['mid']));
        //上级第一代会员
        $member1 = $this->getInfo('member',array('uniacid' => $_W['uniacid'],'id' => $member['agentid']));
        if (!empty($member1)){
            if (!empty($goods)){
                foreach ($goods as $k=>$v) {
                    $commission = $this->getInfo('goods',array(['uniacid'] => $_W['uniacid'],'id' => $v['goodsid']),array('commission1_rate','commission1_pay','commission2_rate','commission2_pay'));
                    $price = $v['price'] * $v['total'];
                    //第一代返利
                    if ($commission['commission1_pay'] > 0){
                        $onefanli[$k] = $commission['commission1_pay'];
                    }else{
                        if ($commission['commission1_rate'] > 0){
                            $onefanli[$k] = $commission['commission1_rate'] * $price / 100;
                        }else{
                            $onefanli[$k] = 0;
                        }
                    }
                    //第二代返利
                    if ($commission['commission2_pay'] > 0){
                        $twofanli[$k] = $commission['commission2_pay'];
                    }else{
                        if ($commission['commission2_rate'] > 0){
                            $twofanli[$k] = $commission['commission2_rate'] * $price / 100;
                        }else{
                            $twofanli[$k] = 0;
                        }
                    }
                }
                $onefanliprice = array_sum($onefanli);
                $twofanliprice = array_sum($twofanli);
            }
            m('member')->setCredit($member1['openid'], 'credit2', $onefanliprice, array(0, '第一代购买返利:' . $onefanliprice));
            //上级第二代会员
            $member2 = $this->getInfo('member',array('uniacid' => $_W['uniacid'],'id' => $member1['agentid']));
            if (!empty($member2)){
                m('member')->setCredit($member2['openid'], 'credit2', $twofanliprice, array(0, '第二代购买返利:' . $twofanliprice));
            }
        }
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据会员openid查找每个季度的订单
     * @goods string 所有商品
     * @uniacid string 公众号ID
     * @orderid string 订单ID
     * @return result(string,修改结果)
     */
    public function selfRebate($goods,$uniacid,$orderid)
    {
        if (!empty($goods)){
            $num = 0;
            foreach ($goods as $k=>$v) {
                if ($v['selfRebate'] == 1){
                    $num += $v['price'];
                }
            }
        }
        $result = $this->update('order',array('need_price' => $num),array('uniacid' => $uniacid,'id' => $orderid));
        return $result;
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据会员openid查找每个季度的订单
     * @openid string 会员openid
     * @uniacid string 公众号ID
     * @return result(string,订单总额)
     */
    public function getAllPrice($openid,$uniacid)
    {
        $orders = $this->getInfoAll('order',array('openid' => $openid,'uniacid' => $uniacid),array('need_price','status','createtime'));
        if (!empty($orders)){
            foreach ($orders as $k=>$v){
                if ($v['status'] != -1 && $v['status'] != 0){
                    $data[$k] = $v;
                    $data[$k]['price'] = $v['need_price'];
                }
            }
            array_multisort(array_column($data,'createtime'),SORT_ASC,$data);
            $firsty = date('Y',$data[0]['createtime']);
            $firstm = date('m',$data[0]['createtime']);
            $nowy = date('Y',time());
            $nowm = date('m',time());
            $numm = $nowm - $firstm + ($nowy - $firsty) * 12;
            $number = $nowy - $firsty;
            if (!empty($data)){
                foreach ($data as $k=>$v) {
                    $data[$k]['y'] = date('Y',$v['createtime']);
                    $data[$k]['m'] = date('m',$v['createtime']);
                }
            }
            for ($i=0; $i <= $number; $i++){
                $list[$i] = $this->monthYear(($firsty + $i),$firstm,$data);
            }
            return $list;
        }
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据会员openid,返回年份季度结构
     * @openid string 会员openID
     * @uniacid string 公众号id
     * @return result(array,结构数据)
     */
    public function annualQuarter($openid,$uniacid)
    {
        $member = m("member")->getMember($openid, true);
        $list = $this->getAllPrice($openid,$uniacid);
        if (!empty($list)){
            foreach ($list as $k=>$v) {
                if (!empty($list[$k])){
                    foreach ($list[$k] as $k1=>$v1) {
                        $num = 0;
                        if (!empty($list[$k][$k1])){
                            foreach ($list[$k][$k1] as $k2=>$v2) {
                                $num += $v2['price'];
                            }
                        }
                        $list[$k][$k1]['price'] = $num;
                        $list[$k][$k1]['fanli'] = $this->selfPurchaseReturn($num);
                        $status = $this->getInfo('price_jidu',array('uniacid' => $uniacid,'openid' => $openid,'time' => ($list[0][0][0]['y'] + $k).'-'.($k1 + 1)));
                        if (empty($status)){
                            $list[$k][$k1]['status'] = 0;
                        }else{
                            $list[$k][$k1]['status'] = 1;
                        }
                    }
                }
            }
        }
        return $list;
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据季度自购金额返回返利金额
     * @price string 季度金额
     * @return result(string,返利金额)
     */
    public function selfPurchaseReturn($price)
    {
        global $_W;
        $list = $this->getInfoAll('performance_level',array('uniacid' => $_W['uniacid']));
        if (!empty($list)){
            array_multisort(array_column($list,'levelname'),SORT_ASC,$list);
            foreach ($list as $k=>$v) {
                if ($price >= $v['levelname'] && $price > 0){
                    $result = $price * $v['commission'] / 100;
                }
            }
            return $result;
        }else{
            return 0;
        }
    }

    /**
     * 索罗万象二次开发
     * 作者: @下雨吧จุ๊บ
     * @function 根据年月份计算一年四个季度的订单
     * @year string 年
     * @month string 月
     * @data array 所有订单
     * @return array(季度订单)
     */
    public function monthYear($year,$month,$data)
    {
        switch ($month){
            case 1:
                for ($i = 0; $i <= 3; $i++){
                    $starttime = strtotime($year.'-'.($month + $i * 3));
                    if ($i == 3){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 2:
                for ($i = 0; $i <= 3; $i++){
                    if ($i == 0){
                        $starttime = strtotime($year.'-'.($month + $i * 3));
                    }else{
                        $starttime = strtotime($year.'-'.($month - 1 + $i * 3));
                    }
                    if ($i == 3){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month - 1 + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 3:
                for ($i = 0; $i <= 3; $i++){
                    if ($i == 0){
                        $starttime = strtotime($year.'-'.($month + $i * 3));
                    }else{
                        $starttime = strtotime($year.'-'.($month - 2 + $i * 3));
                    }
                    if ($i == 3){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month - 2 + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 4:
                for ($i = 0; $i <= 2; $i++){
                    $starttime = strtotime($year.'-'.($month + $i * 3));
                    if ($i == 2){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 5:
                for ($i = 0; $i <= 2; $i++){
                    if ($i == 0){
                        $starttime = strtotime($year.'-'.($month + $i * 3));
                    }else{
                        $starttime = strtotime($year.'-'.($month - 1 + $i * 3));
                    }
                    if ($i == 2){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month - 1 + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 6:
                for ($i = 0; $i <= 2; $i++){
                    if ($i == 0){
                        $starttime = strtotime($year.'-'.($month + $i * 3));
                    }else{
                        $starttime = strtotime($year.'-'.($month - 2 + $i * 3));
                    }
                    if ($i == 2){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month - 2 + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 7:
                for ($i = 0; $i <= 1; $i++){
                    $starttime = strtotime($year.'-'.($month + $i * 3));
                    if ($i == 1){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 8:
                for ($i = 0; $i <= 1; $i++){
                    if ($i == 0){
                        $starttime = strtotime($year.'-'.($month + $i * 3));
                    }else{
                        $starttime = strtotime($year.'-'.($month - 1 + $i * 3));
                    }
                    if ($i == 1){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month - 1 + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 9:
                for ($i = 0; $i <= 1; $i++){
                    if ($i == 0){
                        $starttime = strtotime($year.'-'.($month + $i * 3));
                    }else{
                        $starttime = strtotime($year.'-'.($month - 2 + $i * 3));
                    }
                    if ($i == 1){
                        $stoptime = strtotime(($year+1).'-1');
                    }else{
                        $stoptime = strtotime($year.'-'.($month - 2 + ($i + 1) * 3));
                    }
                    if (!empty($data)){
                        foreach ($data as $k=>$v) {
                            if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                                $list[] = $v;
                            }
                        }
                        $orders[$i] = $list;
                        $list = '';
                    }
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 10:
                $starttime = strtotime($year.'-'.$month);
                $stoptime = strtotime(($year+1).'-1');
                if (!empty($data)){
                    foreach ($data as $k=>$v) {
                        if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                            $list[] = $v;
                        }
                    }
                    $orders[0] = $list;
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 11:
                $starttime = strtotime($year.'-'.$month);
                $stoptime = strtotime(($year+1).'-1');
                if (!empty($data)){
                    foreach ($data as $k=>$v) {
                        if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                            $list[] = $v;
                        }
                    }
                    $orders[0] = $list;
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
            case 12:
                $starttime = strtotime($year.'-'.$month);
                $stoptime = strtotime(($year+1).'-1');
                if (!empty($data)){
                    foreach ($data as $k=>$v) {
                        if ($v['createtime'] >= $starttime && $v['createtime'] <= $stoptime){
                            $list[] = $v;
                        }
                    }
                    $orders[0] = $list;
                }
                if (empty($orders)){
                    return array();
                }else{
                    return $orders;
                }
                break;
        }
    }

}